package com.jspgou.cms.action.front;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.cms.entity.Noctice;
import com.jspgou.cms.entity.Product;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.manager.NocticeMng;
import com.jspgou.cms.manager.ProductMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.FrontUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.core.entity.User;
import com.jspgou.core.entity.Website;
import com.jspgou.core.manager.UserMng;
@Controller
public class NocticeAct {
	
	@RequestMapping(value="/noctice/v_noctice.jspx", method = RequestMethod.POST)
	public String index(Long pId, Long userId,Long productId, Noctice bean, HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = CmsUtils.getMember(request);
		if (member == null) {
			return "redirect:../login.jspx";
		}
		Product product = productMng.findById(productId);	
		bean.setProduct(product);		
		bean.setRegisterTime(new Date());
		User user = userMng.findById(member.getMember().getUser().getId());
		bean.setUser(user);	
		bean = nocticeMng.save(bean);
		ShopFrontHelper.setCommonData(request, model, web);
		return "redirect:" + productMng.findById(productId).getUrl();
	}

	@Autowired
	private ProductMng productMng;
	@Autowired
	private UserMng userMng;
	@Autowired
	private NocticeMng nocticeMng;

}
